/*******************************************************************************
 * Авторское право (c) 2002, 2015 принадлежит Innoopract Informationssysteme GmbH
 * и другим компаниям.
 * Проект RAP 3.27.0. Оригинальный файл (лицензия EPL v1.0):
 * org.eclipse.rap.rwt.engine.RWTServlet.java
 ******************************************************************************/
package erwiki.api.rwc.engune;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import erwiki.api.auth.ISessionMonitor;
import erwiki.api.core.Engine;
import erwiki.api.core.WikiSession;
import erwiki.api.rwc.context.internal.service.ContextProviderW;
import erwiki.api.rwc.context.internal.service.ServiceContextW;

public abstract class RwcServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		handleDoGet(request, response);
	}

	private void handleDoGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		ServiceContextW context = new ServiceContextW(getEngine(), getSessionMonitor().getWikiSession(request), //
				request, response);
		ContextProviderW.setContext(context);
		try {
			serviceDoGet(request, response);
		} finally {
			ContextProviderW.disposeContext();
		}
	}

	protected abstract Engine getEngine();

	protected abstract ISessionMonitor getSessionMonitor();

	protected abstract void serviceDoGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException;

	private void prepareWikiSession(ServiceContextW context) {
	}

}
